/**
 * 
 */
package org.abettor.smartswt.factory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.abettor.smartswt.util.StaticProperty;

/**
 * WidgetListener类的代理
 * @author abettor
 *
 */
public class WidgetListenerProxy
{
	//	用于保存监听器的对象
	private WidgetListener widgetListener = null;
	//	用于存储组件监听器参数类的List
	private List<Class> classes = Collections.synchronizedList(new ArrayList<Class>());
	//	用于存储组件监听器参数实例的List
	private List<Object> objects = Collections.synchronizedList(new ArrayList<Object>());

	/**
	 * 默认构造函数
	 *
	 */
	public WidgetListenerProxy()
	{
		widgetListener = new WidgetListener();
	}

	/**
	 * 设置监听器类型，如MouseListener
	 * @param listenerType 监听器类型名称
	 */
	public void setListenerType(String listenerType)
	{
		widgetListener.setListenerName(listenerType);
	}

	/**
	 * 添加监听器所使用的参数
	 * @param parameterClass 参数类型名称
	 * @param parameter 参数值
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalArgumentException 
	 * @throws SecurityException 
	 */
	public void addListenerParameter(String parameterClass, String parameter)
	throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchFieldException
	{
		//	过滤并设置参数类型与参数实例
		Class clazz = null;
		Object object = null;
		if("boolean".equals(parameterClass))
		{
			clazz = Boolean.TYPE;
			object = new Boolean(parameter);
		}
		else if("byte".equals(parameterClass))
		{
			clazz = Byte.TYPE;
			object = new Byte(parameter);
		}
		else if("char".equals(parameterClass))
		{
			clazz = Character.TYPE;
			object = parameter.charAt(0);
		}
		else if("double".equals(parameterClass))
		{
			clazz = Double.TYPE;
			object = new Double(parameter);
		}
		else if("float".equals(parameterClass))
		{
			clazz = Float.TYPE;
			object = new Float(parameter);
		}
		else if("int".equals(parameterClass))
		{
			clazz = Integer.TYPE;

			//	检查是否为静态常量
			if(parameter.indexOf('.') == -1)
			{
				object = new Integer(parameter);
			}
			else
			{
				object = StaticProperty.getStaticValue(parameter);
			}
		}
		else if("long".equals(parameterClass))
		{
			clazz = Long.TYPE;

			//	检查是否为静态常量
			if(parameter.indexOf('.') == -1)
			{
				object = new Long(parameter);
			}
			else
			{
				object = StaticProperty.getStaticValue(parameter);
			}
		}
		else if("short".equals(parameterClass))
		{
			clazz = Short.TYPE;
			object = new Short(parameter);
		}
		else if("string".equals(parameterClass))
		{
			clazz = String.class;

			//	首先假设为静态常量，出现异常后进行补救
			try
			{
				object = StaticProperty.getStaticValue(parameter);
			}
			catch(Exception e)
			{
				object = parameter;
			}
		}
		else
		{
			clazz = Class.forName(parameterClass);
			object = Class.forName(parameter).newInstance();
		}

		classes.add(clazz);
		objects.add(object);
	}

	/**
	 * 获取配置完成监听器对象
	 * @return 配置完成的监听器对象
	 */
	public WidgetListener getWidgetListener()
	{
		int l = classes.size();
		Class[] c = new Class[l];
		for(int i = 0; i < l; i ++)
		{
			c[i] = classes.get(i);
		}
		widgetListener.setListenerClasses(c);
		widgetListener.setListenerObjects(objects.toArray());
		return widgetListener;
	}
}
